<template>
  <div class="app-container">
    <el-input
      v-model="filterText"
      placeholder="输入查询条件"
      style="margin-bottom:30px;" />

    <el-tree
      ref="subjectTree"
      :data="subjectList"
      :props="defaultProps"
      :filter-node-method="filterNode"
      style="margin-top:10px;" />
  </div>
</template>

<script>
import subject from '../../api/subject'

export default {
  data() {
    return {
      filterText: '',
      subjectList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.subjectTree.filter(val)
    }
  },
  created() {
    this.getNodeList()
  },
  methods: {
    // 获取远程数据
    getNodeList() {
      subject.getNestedTreeList().then(res => {
        this.subjectList = res.data.items
      })
    },

    /**
     * 过滤节点
     * @param value 输入的过滤条件
     * @param data tree绑定的数据
     * @returns {boolean} 是否需要显示 true-显示 false-不显示
     */
    filterNode(value, data) {
      if (!value) return true
      return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss">

</style>
